1046. Last Stone Weight

题目 1046. Last Stone Weight

image-c497fc5d

思路分析

image-89f8327b

优先队列实现

代码实现

class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        for(int stone:stones){
            pq.offer(stone);
        }

        while(pq.size()>1){
            int x = pq.poll();
            int y = pq.poll();

            if(x>y){
                pq.offer(x-y);
            }
        }

        return pq.isEmpty() ? 0 : pq.peek();
    }
}

同类题型

视频讲解